#include<type.h>
#include<driver.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
int disk_fd;
acoral_32 emu_disk_write(void *data,acoral_size size,acoral_size offset,acoral_time tm_out){
	offset *= 512;
	lseek(disk_fd,offset,SEEK_SET);
	if(write(disk_fd,data,size)!=size)
		return -1;
	return 0;
}

acoral_32 emu_disk_read(void *data,acoral_size size,acoral_size offset,acoral_time tm_out){
	int i;
	acoral_u8 *tmp;
	i=0;

	offset *= 512;
	lseek(disk_fd,offset,SEEK_SET);
	if(read(disk_fd,data,size)!=size)
		return -1;
/*
	acoral_debug("\n===== %x =====", offset);
	tmp = (acoral_u8*)data;
	while(i<512)
	{
		acoral_debug("%x ", tmp[i]);
		i++;
		if (!(i%16))
			acoral_debugs("\n");
	}
*/
	return 0;
}

acoral_32 emu_disk_open(){
	disk_fd=open("acoral.disk",O_RDWR);
	if(disk_fd<0)
		return -1;
	return 0;
}

acoral_32 emu_disk_close(){
	close(disk_fd);
	return 0;
}

acoral_dev_ops_t disk_ops={
  	.write=emu_disk_write,
  	.read=emu_disk_read,
  	.open=emu_disk_open,
  	.close=emu_disk_close,
};

void disk_init(){
  	acoral_drv_register(&disk_ops,"disk");
}
